#include "Test.h"

void CTestObj::CreateBuffers( )
{
    /* Initializing vertices */
//    int i, j, ind;
    int NumStacks = 20;
    int NumSlices = 20;
    float _2PI = 2.0f * 3.1415926f;
    m_uVertexCount = 4;//NumSlices * (NumStacks + 1);
    m_uIndexCount  = 6;//6 * NumSlices * NumStacks;
    VertexDecl *Vertices = new VertexDecl[m_uVertexCount];
    UINT32     *Indices  = new UINT32[m_uIndexCount];

    /*for (i = 0; i <= NumStacks; i++)
    {
        for (j = -NumSlices / 2; j < NumSlices / 2; j++)
        {
            ind = (i) * NumSlices + j + NumSlices / 2;
            Vertices[ind].vPos.y = 5.0f * cos(float(i) / NumStacks * 3.1415926f );
            Vertices[ind].vPos.x = 5.0f * cos(float(j) / NumSlices * (float)_2PI) * sin(float(i) / NumStacks * 3.1415926f );
            Vertices[ind].vPos.z = 5.0f * sin(float(j) / NumSlices * (float)_2PI) * sin(float(i) / NumStacks * 3.1415926f );
            Vertices[ind].vPos.w = 1.0f;
            Vertices[ind].vTexCoord.x = float(j + NumSlices / 2) / NumSlices;
            Vertices[ind].vTexCoord.y = float(i) / NumStacks;
        }
    }

    ind = 0;
    for (i = 1; i <= NumStacks; i++)
    {
        for (j = 1; j < NumSlices; j++)
        {
            Indices[ind++] = i       * NumSlices + j      ;
            Indices[ind++] = (i - 1) * NumSlices + j      ;
            Indices[ind++] = (i - 1) * NumSlices + (j - 1);

            Indices[ind++] = i       * NumSlices + j      ;
            Indices[ind++] = (i - 1) * NumSlices + (j - 1);
            Indices[ind++] = i       * NumSlices + (j - 1);
        }

        Indices[ind++] = i       * NumSlices    ;
        Indices[ind++] = (i - 1) * NumSlices    ;
        Indices[ind++] = i       * NumSlices - 1;

        Indices[ind++] = i       * NumSlices    ;
        Indices[ind++] = i       * NumSlices - 1;
        Indices[ind++] = (i + 1) * NumSlices - 1;
    }*/
    Vertices[0].vPos = D3DXVECTOR4(0.0, -5.0, -5.0, 1.0);
    Vertices[1].vPos = D3DXVECTOR4(0.0, -5.0, 5.0, 1.0);
    Vertices[2].vPos = D3DXVECTOR4(0.0, 5.0, 5.0, 1.0);
    Vertices[3].vPos = D3DXVECTOR4(0.0, 5.0, -5.0, 1.0);

    Vertices[0].vTexCoord = D3DXVECTOR2(0.0, 0.0);
    Vertices[1].vTexCoord = D3DXVECTOR2(0.0, 1.0);
    Vertices[2].vTexCoord = D3DXVECTOR2(1.0, 1.0);
    Vertices[3].vTexCoord = D3DXVECTOR2(1.0, 0.0);


    Indices[0] = 0;
    Indices[1] = 1;
    Indices[2] = 2;
    Indices[3] = 0;
    Indices[4] = 2;
    Indices[5] = 3;
    
    m_pIAI->CreateVertexBuffer((mVoid*)Vertices, m_uVertexCount * sizeof(VertexDecl), sizeof(VertexDecl));
    m_pIAI->CreateIndexBuffer((mVoid*)Indices, m_uIndexCount * sizeof(UINT32), TUINT32);
    D3DXMATRIX mView;
    D3DXMATRIX mProj;
    D3DXVECTOR3 vPos(15.0, 0.0, 0.0);
    D3DXVECTOR3 vLookAt(0.0, 0.0, 0.0);
    D3DXVECTOR3 vUp(0.0f, 1.0f, 0.0f);
    D3DXMatrixLookAtLH(&mView, &vPos, &vLookAt, &vUp);
    D3DXMatrixPerspectiveFovLH(&mProj, 53.4f * 3.1415f/180.0f, 1.33f, 0.1f, 200.0f);
    D3DXMatrixMultiply(&m_mViewProj, &mView, &mProj);
}

CTestObj::CTestObj( )
{
    if (g_ApiVersion == V_D3D10)
    {
        m_pVShader = CVShader::CreateVShader("Test/Shader10.vsh");
        m_pPShader = CPShader::CreatePShader("Test/Shader10.psh");
    }
    else
    {
        m_pVShader = CVShader::CreateVShader("Test/Shader9.vsh");
        m_pPShader = CPShader::CreatePShader("Test/Shader9.psh");
    }
    
    m_pVShader->Compile();    
    m_pPShader->Compile();
    m_pTexture = CTexture2D::CreateTexture2D();
    m_pRS      = CRasterizerState::CreateRS();
    m_pTexture->CreateFromFile("Test/Texture.dds");
    CreateBuffers();
    m_pVertexDeclaration->AddElement("POSITION", 0, TFLOAT4, 0);
    m_pVertexDeclaration->AddElement("TEXCOORD", 0, TFLOAT2, 0);
    m_pVertexDeclaration->Create(m_pVShader->ShaderCode(), m_pVShader->ShaderCodeSize());    
    m_pPShader->SendTexture("g_txDiffuse", m_pTexture);
    m_pVShader->SendConstantValue("g_mViewProj", (mVoid*)&m_mViewProj, sizeof(D3DXMATRIX), Immutable );
}

void CTestObj::Render( )
{    
    m_pVertexDeclaration->Setup();
    m_pRS->Setup();
    m_pIAI->IASetupBuffers();
    //m_pVShader->SendConstantValue("g_mViewProj", (mVoid*)&m_mViewProj, sizeof(D3DXMATRIX), Immutable );
    m_pVShader->Bind();
    if (g_ApiVersion == V_D3D10)
        g_D3DDevice.pD3D10Device->GSSetShader(NULL);
    m_pPShader->Bind();
    m_pDrawInterface->SetPrimitiveType(TTRIANGLELIST);
    m_pDrawInterface->DrawIndexed(m_uIndexCount, 0, m_uVertexCount, 0);
}

CTestObj::~CTestObj( )
{
    delete m_pRS;
    delete m_pTexture;
    delete m_pVShader;
    delete m_pPShader;
}
